package com.example.studywords.utils;

import android.content.Context;

import com.example.studywords.bean.TranslateBean;
import com.example.studywords.bean.WordBean;
import com.example.studywords.bean.WordInfoBean;
import com.example.studywords.bean.WordTransRelationBean;
import com.example.studywords.sql.MyDatabase;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SqlUtil {
    /**
     * 插入关系，包括插入word,translation，然后插入二者关系。同时需要判断word已存在的情况
     * @param word
     * @param translations 可能包含多个,用空格分开
     * @param context
     */
    public static void insertRelation(String word, String translations,String wordNote, Context context){
        int wordId = MyDatabase.getDatabase(context).wordDao().selectIdByContent(word);
        if(wordId==0){
            wordId = insertWordAndInfo(word,wordNote,context);
        }
        String[] translation = translations.split(" ");
        for(String ATrans : translation){
            int translationId = insertTranslation(ATrans,context);
            MyDatabase.getDatabase(context).wordTransRelationDao().insert(new WordTransRelationBean(
                    wordId,translationId
            ));
        }
    }
    /**
     * 插入word时也要同步创建其Info!
     * @return 新建word的Id
     */
    private static int insertWordAndInfo(String word,String wordNote,Context context){
        MyDatabase.getDatabase(context).wordDao().insert(new WordBean(word));
        int wordId = MyDatabase.getDatabase(context).wordDao().selectIdByContent(word);
        if(!wordNote.equals("")){
            MyDatabase.getDatabase(context).wordInfoDao().insert(new WordInfoBean(wordId,wordNote));
        }else{
            MyDatabase.getDatabase(context).wordInfoDao().insert(new WordInfoBean(wordId));
        }

        return wordId;
    }
    private static int insertTranslation(String translation,Context context){
        MyDatabase.getDatabase(context).translateDao().insert(new TranslateBean(translation));
        return MyDatabase.getDatabase(context).translateDao().selectIdByContent(translation);
    }

    public static void updateWordInfo(int wordId,boolean isCorrect,Context context){
        WordInfoBean wordInfoBean = MyDatabase.getDatabase(context).wordInfoDao().selectWordInfo(wordId);
        if(isCorrect)
            wordInfoBean.setCorrectNum(wordInfoBean.getCorrectNum()+1);
        else
            wordInfoBean.setCorrectNum(wordInfoBean.getCorrectNum()==0 ? 0 : wordInfoBean.getCorrectNum()-1);
        wordInfoBean.setLastStudyTime(DateUtil.getCurrentDate());
        MyDatabase.getDatabase(context).wordInfoDao().update(wordInfoBean);
    }

    public static List<TranslateBean> selectTranslationByWordId(int wordId,Context context){
        List<WordTransRelationBean> relations = MyDatabase.getDatabase(context).wordTransRelationDao().
                selectRelation(wordId);
        //根据关系查翻译
        List<TranslateBean> translations = new ArrayList<>();
        for(WordTransRelationBean relation : relations){
            translations.add(MyDatabase.getDatabase(context).translateDao().
                    selectTranslation(relation.getChildTranslateId()));
        }
        return translations;
    }
}
